﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController : ITXController
    {
        
        public List<ABFOLGEModel> ABFS
        {
            get
            {
                if (!ENVMAIN.ContainsKey("ABFS"))
                {
                    ENVMAIN.Add("ABFS", null);
                }
                return (List<ABFOLGEModel>)ENVMAIN["ABFS"];
            }
            set
            {
                ENVMAIN["ABFS"] = value;
            }
        }
        public List<AMSTAMMModel> AMS
        {
            get
            {
                if (!ENVMAIN.ContainsKey("AMS"))
                {
                    ENVMAIN.Add("AMS", AMLOAD());
                }
                return (List<AMSTAMMModel>)ENVMAIN["AMS"];
            }
            set
            {
                ENVMAIN["AMS"] = value;
            }
        }
        public List<HRSTAMMModel> HRS
        {
            get
            {
                if (!ENVMAIN.ContainsKey("HRS"))
                {
                    ENVMAIN.Add("HRS", HRLOAD());
                }
                return (List<HRSTAMMModel>)ENVMAIN["HRS"];
            }
            set
            {
                ENVMAIN["HRS"] = value;
            }
        }

        public ABFOLGEController()
        {


        }

        [ChildActionOnly]
        public ActionResult MRSABF(string returnUrl)
        {
            ModelState.Clear();
            UPViewBag();
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(MABF);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MRSABF(VVTLIEFModel model, string returnUrl)
        {
            StartNewJob();
            try
            {
                //WBW.POSTCHG();
            }
            catch (Exception ex)
            {
                //VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }


        public void StartNewJob()
        {
            MABF = new ABFOLGEModel(MABF);
            WRUNID = MABF.WRUNID;
            MABF.NEWUSER = CUSER;
            INFO = "请扫描";
        }
        public void GETABF()
        {
            var result = ABFS.Where(p => p.WRUNID.Trim() == WRUNID.Trim());
            foreach (var step in result)
            {
                MABF = step;
            }
        }
        public void UPViewBag()
        {
            ViewBag.IsPDCReady = MABF.IsPDCReady;
            ViewBag.IsABPReady = MABF.IsABPReady;
            ViewBag.IsAMReady = MABF.IsAMReady;
            ViewBag.IsSTEPReady = MABF.IsSTEPReady;
            ViewBag.IsStaffReady = MABF.IsStaffReady;
            ViewBag.IsStartReady = MABF.IsStart;
            ViewBag.IsGotABE = MABF.IsGotABE;
            ViewBag.IsEndReady = MABF.IsEndReady;
        }

        public List<AMSTAMMModel> AMLOAD()
        {

            List<AMSTAMMModel> ams = new List<AMSTAMMModel>();
            DataSet SReult = ABP.LOADAM(PM);

            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                AMSTAMMModel m = new AMSTAMMModel();
                m.MACHINEID = row["AMBDEMA"].ToString().Trim();
                m.DES = Helper.Convert8859P1ToGB2312(row["AMBEZ"].ToString());
                m.MKRS = row["AMNRKRS"].ToString();
                m.MLFD = row["AMLFDNR"].ToString();
                m.MPOS = row["AMPOS"].ToString();
                m.KKRS = row["AMKSTKRS"].ToString();
                m.KKST = row["AMKST"].ToString();
                m.MLO = row["AMLO"].ToString();
                m.MLB = row["AMLB"].ToString();
                m.MLP = row["AMLPLATZ"].ToString();
                ams.Add(m);
            }
            return ams;
        }

        public ABPOSModel LOADABP(string _ABPAUF)
        {
            PM["AUFTYP"] = "V";
            PM["KRS"] = _ABPAUF.Substring(0, 2);
            PM["LFD"] = _ABPAUF.Substring(2, 6);
            PM["POS"] = _ABPAUF.Substring(8, 3);
            SReult = ABP.LOAD4ABP(PM);
            ABPOSModel MABP = null;
            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                MABP = new ABPOSModel();
                MABP.AUFART = row["ABKAUFART"].ToString();
                MABP.ABPAUF = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                MABP.WAKZ = row["ABPWAKZ"].ToString();
                MABP.ART = row["ABPART"].ToString();
                MABP.VAR = row["ABPVAR"].ToString();
                MABP.DESS = row["ABPDESS"].ToString();
                MABP.COLORIT = row["ABPCOLORIT"].ToString();
                MABP.FARBE = row["ABPFARBE"].ToString();
                MABP.BESCHICH = row["ABPBESCHICH"].ToString();
            }
            return MABP;
        }
        public List<ABARTEModel> LOADABE(Dictionary<string, object> pm)
        {
            SReult = ABP.LOADABE(pm);
            List<ABARTEModel> ABES = new List<ABARTEModel>();
            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                ABARTEModel ABE = new ABARTEModel();
                ABE.MSETID = row["BDEKEY"].ToString();
                ABE.MSETNAME = Helper.GetGB2312Value(row["BDEDES"].ToString()).Trim();
                ABE.MSETTXTV = row["ABEWERTX"].ToString();//目标值文本
                ABE.MSETNUMV = row["ABEWERTN"].ToString();//目标值数字
                string nv = row["ABEWERTN"].ToString();
                if (nv != "0000000000000000")
                {
                    ABE.MSETTXTV = (Convert.ToDouble(nv) * 0.000001).ToString();
                }
                ABE.MNOWTXTV = row["ABEIWERTX"].ToString();//实际值
                ABE.MNOWNUMV = row["ABEIWERTN"].ToString();//实际值
                nv = row["ABEIWERTN"].ToString();
                if (nv != "0000000000000000")
                {
                    ABE.MNOWTXTV = (Convert.ToDouble(nv) * 0.000001).ToString();
                }
                ABE.IsGet = false;
                ABES.Add(ABE);
            }
            return ABES;
        }
        public void LOADABFS(DataTable TBABF)
        {
            if (ABFS == null)
            {
                ABFS = new List<ABFOLGEModel>();
            }
            ABFS.Clear();
            foreach (DataRow row in TBABF.Rows)
            {

                ABFOLGEModel m = new ABFOLGEModel();
                m.MABP = LOADABP(row["ABFAUFKRS"].ToString() + row["ABFAUFLFD"].ToString() + row["ABFAUFPOS"].ToString());

                m.NEWUSER = CUSER.PadRight(8);
                m.FSTEP = row["ABFAFOLGE"].ToString();
                m.KRS = row["ABFAUFKRS"].ToString();
                m.LFD = row["ABFAUFLFD"].ToString();
                m.POS = row["ABFAUFPOS"].ToString();

                m.ABPAUF = row["ABFAUFKRS"].ToString() + row["ABFAUFLFD"].ToString() + row["ABFAUFPOS"].ToString();
                m.VFART = row["ABPART"].ToString() + row["ABPFARBE"].ToString();
                m.WAKZ = row["ABPWAKZ"].ToString();
                m.ART = row["ABPART"].ToString();
                m.VAR = row["ABPVAR"].ToString();
                m.FARBE = row["ABPFARBE"].ToString();
                m.QTYPROD = Convert.ToDouble(row["ABFIMG"].ToString()) * 0.001;//实际数量
                m.PLANQTY = Convert.ToDouble(row["ABFSOLLMG"].ToString()) * 0.001;//计划数量
                m.DTSTART = row["ABFISTARTDAT"].ToString();
                m.TMSTART = row["ABFISTARTZT"].ToString();
                m.DTEND = row["ABFIENDDAT"].ToString();
                m.TMEND = row["ABFIENDZT"].ToString();
                m.STATUS = row["ABFSTATUS"].ToString();
                string amlfd = row["ABFIMLFDNR"].ToString();
                string smlfd = row["ABFMLFDNR"].ToString();
                if (amlfd == "0000")
                { 
                    m.MAM.KKRS = row["ABFKSTKRS"].ToString();
                    m.MAM.KKST = row["ABFKST"].ToString();
                    m.MAM.MKRS = row["ABFMNRKRS"].ToString();
                    m.MAM.MLFD = row["ABFMLFDNR"].ToString();
                    m.MAM.MPOS = row["ABFMPOS"].ToString();
                }
                else
                {
                    m.MAM.KKRS = row["ABFKSTKRS"].ToString();
                    m.MAM.KKST = row["ABFKST"].ToString();
                    m.MAM.MKRS = row["ABFIMNRKRS"].ToString();
                    m.MAM.MLFD = row["ABFIMLFDNR"].ToString();
                    m.MAM.MPOS = row["ABFIMPOS"].ToString();
                }


                m.ABFAGANG = Helper.GetGB2312Value(row["ABFAGANG"].ToString()).Trim();
                m.ABFFIX = row["ABFFIX"].ToString();
                m.ABFMANUELKZ = row["ABFMANUELKZ"].ToString();
                m.ABFEVARHNR = row["ABFEVARHNR"].ToString();
                m.ABFEVARUNR = row["ABFEVARUNR"].ToString();
                m.ABFME = row["ABFME"].ToString();

                PM["STEP"] = m.FSTEP;
                PM["KRS"] = m.KRS;
                PM["LFD"] = m.LFD;
                PM["POS"] = m.POS;
                PM["MKRS"] = m.MAM.MKRS;
                PM["MLFD"] = m.MAM.MLFD;
                PM["MPOS"] = m.MAM.MPOS;
                m.ABES = LOADABE(PM);
                m.IsGotABE = false;
                ABFS.Add(m);
                MABF.IsABPReady = true;
            }
        }
        public void SHIFT()
        {
            PM["PDCSHIFT"] = 1;
            double time = Convert.ToDouble((string)PM["PDCTIME"]);
            switch (app.company)
            {
                case "JLT":
                    if (time > 800)
                    {
                        PM["PDCSHIFT"] = 1;
                    }
                    if (time > 1700)
                    {
                        PM["PDCSHIFT"] = 2;
                    }
                    if (time > 2300)
                    {
                        PM["PDCSHIFT"] = 3;
                    }
                    break;
            }
        }
        public void PREPAREPM(ABFOLGEModel model)
        {
            PM["LO"] = MABF.MAM.MLO;
            PM["LB"] = MABF.MAM.MLB;
            PM["LP"] = MABF.MAM.MLP;
            PM["PDCTYPE"] = MABF.PDCTYPE;
            PM["PROG"] = MABF.PROG;
            PM["KRS"] = MABF.KRS;
            PM["LFD"] = MABF.LFD;
            PM["POS"] = MABF.POS;
            PM["STEP"] = MABF.FSTEP;
            PM["PLANQTY"] = MABF.PLANQTY;
            PM["QTY"] = MABF.QTY;
            PM["KKRS"] = MABF.MAM.KKRS;
            PM["KKST"] = MABF.MAM.KKST;
            PM["MKRS"] = MABF.MAM.MKRS;
            PM["MLFD"] = MABF.MAM.MLFD;
            PM["MPOS"] = MABF.MAM.MPOS;
            PM["ABFME"] = MABF.ABFME;

            PM["MACHINEID"] = MABF.MACHINEID;
            PM["PDCSTAFF"] = MABF.STAFF;
            PM["NEWUSER"] = CUSER.PadRight(8);

            PM["ABFAGANG"] = Helper.ConvertGB2312To8859P1(MABF.ABFAGANG).PadRight(15);
            PM["ABFFIX"] = MABF.ABFFIX;
            PM["ABFMANUELKZ"] = MABF.ABFMANUELKZ;
            PM["ABFEVARHNR"] = MABF.ABFEVARHNR;
            PM["ABFEVARUNR"] = MABF.ABFEVARUNR;
            PM["MKFTP"] = MABF.MKFTP;
            PM["AUFART"] = MABF.MABP.AUFART;
            PM["WAKZ"] = MABF.MABP.WAKZ;
            PM["ART"] = MABF.MABP.ART;
            PM["VAR"] = MABF.MABP.VAR;
            PM["DESS"] = MABF.MABP.DESS;
            PM["COLORIT"] = MABF.MABP.COLORIT;
            PM["FARBE"] = MABF.MABP.FARBE;
            PM["BESCHICH"] = MABF.MABP.BESCHICH.PadRight(10);

        }
    }
}
